package com.cswnode.demo.service.impl;

import com.cswnode.demo.controller.requestVariable.GetSexRatio;
import com.cswnode.demo.dao.DataDao;
import com.cswnode.demo.service.DataService;
import com.cswnode.demo.util.RedisKeyUtil;
import com.cswnode.demo.vo.DataSummary;
import com.cswnode.demo.vo.SexRatio;
import com.cswnode.demo.vo.UnemployedRate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author csw
 * @create 2021/3/22 23:36
 */
@Service
public class DataServiceImpl implements DataService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DataDao dataDao;

    @Override
    public DataSummary getDataSummary() {
        DataSummary dataSummary = (DataSummary) redisTemplate.opsForValue().get(RedisKeyUtil.getDatasummary());
        if (dataSummary == null){
            DataSummary dataSummary1 = new DataSummary();
            dataSummary1.setStudentNum(dataDao.getStudentNum(null));
            dataSummary1.setClassNum(dataDao.getClassNum());
            dataSummary1.setForEmploymentNum(dataDao.getForEmploymentNum());
            dataSummary1.setUnemployedNum(dataDao.getUnemployedNum());
            dataSummary1.setBoyNum(dataDao.getBoyNum(null));
            dataSummary1.setGirlNum(dataDao.getGirlNum(null));
            dataSummary1.setPostNum(dataDao.getPostNum());
            dataSummary1.setProNum(dataDao.getProNum());
            redisTemplate.opsForValue().set(RedisKeyUtil.getDatasummary(),dataSummary1,12, TimeUnit.HOURS);
            return dataSummary1;
        }
        return dataSummary;
    }

    @Override
    public SexRatio getSexRation(GetSexRatio getSexRatio) {
        SexRatio sexRatio = new SexRatio();
        sexRatio.setBodyMax(dataDao.getStudentNum(getSexRatio));
        sexRatio.setBoyNum(dataDao.getBoyNum(getSexRatio));
        sexRatio.setGirlNum(dataDao.getGirlNum(getSexRatio));
        return sexRatio;
    }

    @Override
    public UnemployedRate getUnemployedRate(GetSexRatio getSexRatio) {
        UnemployedRate unemployeRate = dataDao.getUnemployeRate(getSexRatio);
        return unemployeRate;
    }

    @Override
    public List<Map<String, Integer>> getSalary(GetSexRatio getSexRatio) {
        return dataDao.getSalary(getSexRatio);
    }

    @Override
    public List<Map<String, String>> getWorkAddress(GetSexRatio getSexRatio) {
        return dataDao.getWorkAddress(getSexRatio);
    }
}
